package com.swak.paxos;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * Paxos 提交的值;
 * 
 * @author DELL
 */
@Getter
@Setter
@Accessors(chain = true)
public class Value implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final AtomicLong VALUE_ID = new AtomicLong(1);
	public static final byte Flag_NOSEND = 1;

	private long id;
	private String nodeID;
	private int smID;
	private Object value;
	private byte flag;

	public boolean isNull() {
		return value == null;
	}

	@SuppressWarnings("unchecked")
	public <T> T getEstimateValue() {
		return (T) value;
	}

	public Value addFlag(byte flag) {
		this.flag = (byte) (this.flag | flag);
		return this;
	}

	public Value delFlag(byte flag) {
		this.flag = (byte) (this.flag & (~flag));
		return this;
	}

	public boolean isNoSend() {
		return (this.flag & Flag_NOSEND) == Flag_NOSEND;
	}

	public Value clone() {
		return new Value().setId(id).setNodeID(nodeID).setSmID(smID).setValue(value);
	}

	public static Value of() {
		return new Value().setId(VALUE_ID.getAndIncrement());
	}

	public static Value of(byte[] value) {
		return new Value().setId(VALUE_ID.getAndIncrement()).setValue(value);
	}
}
